<?php

namespace Org\Com;

class Mail
{
    var $timeout    = 30;
    var $errors     = array();
    var $priority   = 3; // 1 = High, 3 = Normal, 5 = low
    var $debug      = false;

    var $PluginDir;
    var $mailer;
    var $tpl;
    
    var $_mail_user_dir;     // 用户邮件模板存放路径
    var $_mail_default_dir;  // 默认邮件模板存放路径
    function __construct()
    {
        $this->tpl=\Think\Think::instance('Think\View');
        $this->PluginDir  = LIB_PATH  . 'Org/Com/mail/library/';
        $this->_mail_default_dir = LIB_PATH  . 'Org/Com/mail/template/default/';
        $this->_mail_user_dir    = LIB_PATH  . 'Org/Com/mail/template/user/';
        
    }

/**
 *    获取邮件内容
 *
 *    @author    Garbin
 *    @param     string $mail_tpl
 *    @param     array  $var
 *    @return    array
 */
function get_mail($mail_tpl, $var = array()) {
    $subject = '';
    $message = '';

    $tpl_info = $this->getOne($mail_tpl);

    /* 解析其中变量 */
    $this->tpl->assign('site_name', C('site_name'));
    $this->tpl->assign('site_url', C('SITE_URL'));
    $this->tpl->assign('mail_send_time', date('Y-m-d H:i', time()));
    foreach ($var as $key => $val) {
        $this->tpl->assign($key, $val);
    }
    $subject = $this->tpl->fetch('',$tpl_info['subject']);
    $message = $this->tpl->fetch('',$tpl_info['content']);
    /* 返回邮件 */

    return array(
        'subject' => $subject,
        'message' => $message
    );
}
    function getOne($key)
    {
        $details_data = file_exists($this->_mail_user_dir . $key . '.php') ? include($this->_mail_user_dir . $key . '.php') : include($this->_mail_default_dir . $key . '.php');
        return $details_data;
    }
    

    function mailer($from, $email, $protocol, $host = '', $port = '', $user = '', $pass = '')
    {
        include_once($this->PluginDir . "class.phpmailer.php");
        $this->mailer = new \phpmailer();

        $this->mailer->From     = $email;
        $this->mailer->FromName = $this->_base64_encode($from);

        if ($protocol == MAIL_PROTOCOL_LOCAL)
        {
            /* mail */
            $this->mailer->IsMail();
        }
        else
        {
            /* smtp */
            $this->mailer->IsSMTP();
            $this->mailer->Host     = $host;
            $this->mailer->Port     = $port;
            $this->mailer->SMTPAuth = !empty($pass);
            $this->mailer->Username = $user;
            $this->mailer->Password = $pass;
        }
    }
    /**
     *    发送邮件
     *
     *    @author    Garbin
     *    @param     mixed  $to
     *    @param     string $subject
     *    @param     string $message
     *    @param     int    $priority
     *    @return    void
     */
    function _mailto($to, $subject, $message, $priority = MAIL_PRIORITY_LOW)
    {
        /* 加入邮件队列，并通知需要发送 */
        $mails = array();
        $to_emails = is_array($to) ? $to : array($to);
        foreach ($to_emails as $_to)
        {
            $mails[] = array(
                'mail_to'       => $_to,
                'mail_encoding' => CHARSET,
                'mail_subject'  => $subject,
                'mail_body'     => $message,
                'priority'      => $priority,
                'add_time'      => time(),
            );
        }

        /* 逐条发送 */
        foreach ($mails as $mail)
        {
            $this->send($mail['mail_to'], $mail['mail_subject'], $mail['mail_body'], $mail['mail_encoding'], 1);
        }
        
    }
    function send($mailto, $subject, $content, $charset, $is_html, $receipt = false)
    {
        $this->mailer->Priority     = $this->priority;
        $this->mailer->CharSet      = $charset;
        $this->mailer->IsHTML($is_html);
        $this->mailer->Subject      = $this->_base64_encode($subject);
        $this->mailer->Body         = $content;
        $this->mailer->Timeout      = $this->timeout;
        $this->mailer->SMTPDebug    = $this->debug;
        $this->mailer->ClearAddresses();
        $this->mailer->AddAddress($mailto);

        $res = $this->mailer->Send();
        if (!$res)
        {
            $this->errors[] = $this->mailer->ErrorInfo;
        }
        return $res;
    }

    function _base64_encode($str = '')
    {
        return '=?' . CHARSET . '?B?' . base64_encode($str) . '?=';
    }
}

?>